package com.zhao.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhao.pojo.Datafile;
import com.zhao.pojo.Orders;
import com.zhao.pojo.Transaction;
import com.zhao.pojo.User;
import com.zhao.service.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author : Mr.zhao
 * @Date: 2022/05/14/11:50
 * @Description:
 */
@Controller
@RequestMapping("/transaction")
public class TransactionController {
    @Autowired
    TransactionService transactionService;
    @Autowired
    DatafileService datafileService;
    @Autowired
    RoleService roleService;
    @Autowired
    UserService userService;
    @Autowired
    OrdersService ordersService;
    //下载记录
    @RequestMapping("/data_download")
    public String data_download(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                            @RequestParam(value = "size",required = false,defaultValue = "5")Integer size,
                            @RequestParam(value = "title",required = false)String title,
                            Model model) {
        //判断当前用户身份 用户查自己 管理员和 超级管理员 查全部
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        String roleName = roleService.getUserRoleName(loginUser.getUserId());
        Integer uid = null;
        if (roleName.equals("用户")){
            uid=loginUser.getUserId();
        }
        QueryWrapper<Transaction> wrapper=new QueryWrapper<>();
        wrapper.eq(Objects.nonNull(uid),"buyers",uid);
        Page<Transaction> page1 = new Page<>(pageNum,size);
        IPage<Transaction> iPage = transactionService.page(page1, wrapper);
        for (Transaction transaction : iPage.getRecords()) {
            Datafile datafile = datafileService.getById(transaction.getFileId());
            if (Objects.isNull(datafile)){
                datafile = new Datafile();
                User byId = userService.getById(transaction.getSeller());
                if (Objects.isNull(byId)){
                    datafile.setAuthor("用户已注销");
                }else{
                    datafile.setAuthor(byId.getUserAccount());
                }
                datafile.setTitle("资源已删除");
                datafile.setPrice("资源已删除");
            }
            transaction.setFileName(datafile.getTitle());
            transaction.setAuthor(datafile.getAuthor());
            transaction.setPrice(datafile.getPrice());
        }
//        Page<Datafile> page=datafileService.selectListPage(pageNum, size,title,author,null);
        model.addAttribute("files",iPage);
        return "admin/data_download";
    }

    @RequestMapping("/data_orders")
    public String data_order(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                            @RequestParam(value = "size",required = false,defaultValue = "5")Integer size,
                            @RequestParam(value = "title",required = false)String title,
                            Model model) {
        //判断当前用户身份 用户查自己 管理员和 超级管理员 查全部
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        String roleName = roleService.getUserRoleName(loginUser.getUserId());
        Integer uid = null;
        if (roleName.equals("用户")){
            uid=loginUser.getUserId();
        }
        QueryWrapper<Orders> wrapper=new QueryWrapper<>();
        wrapper.eq(Objects.nonNull(uid),"uid",uid);
        wrapper.orderByDesc("create_time");
        Page<Orders> page1 = new Page<>(pageNum,size);
        IPage<Orders> iPage = ordersService.page(page1, wrapper);
        for (Orders orders : iPage.getRecords()) {
            User user = userService.getById(orders.getUid());
            if (Objects.isNull(user)){
                orders.setAuthor("用户已注销");
                orders.setIphone("用户已注销");
            }
            orders.setAuthor(user.getUserAccount());
            orders.setIphone(user.getUserIphone());
        }
        model.addAttribute("files",iPage);
        return "admin/data_orders";
    }
}
